# import flask,json
# import subprocess,pymysql
# #coding=utf-8
#
# #函    数：通过Api接口将手机相册和录音备份到本地
# #前    提：连接MIUI手机 & 打开手机开发者调试功能 & adb命令可以操作手机
# #参    数：go(字符类型) 执行备份功能
# #请求格式：curl -X POST http://127.0.0.1:8888/jaina?go=backupPhone
# #返回结果：{'msg':'新备份了2个文件!重新备份了10个文件!共有备份文件100个!','msg_code':200}
# #时空复杂度：Jaina
#
# api = flask.Flask(__name__)
# ren='jaian'
# backupNum = 0
# rebackupNum = 0
# allNum = 0
#
# @api.route('/jaina',methods=['post'])
# def go():
#     global ren,backupNum,rebackupNum,allNum
#     go = flask.request.values.get('go')
#
#     if go == "backupPhone":
#         #以数据库为主 判断文件是否存在
#         #1,adb查看文件列表
#         #2,将每一行文件名称和数据库中对比
#         #3,查询有文件，无操作
#         #4,查询无文件，将该文件位置存入数据库，并adb pull到D盘
#         #create table backup(id int(11) NOT NULL AUTO_INCREMENT primary key,filedir varchar(199) not null UNIQUE);
#         #create database redmi_k40 character set utf8 collate utf8_bin
#
#         #相机、截图 1596
#
#         # bm='chcp 65001'
#         # bm = subprocess.Popen(bm,shell=True,stdout=subprocess.PIPE)
#
#         DCIM='adb shell "find /sdcard/DCIM/ -type f | grep -v txt | grep -v dat | grep -v .th"'
#         OutDCIM = subprocess.Popen(DCIM,shell=True,stdout=subprocess.PIPE)
#         #应用保存 838
#         Pictures='adb shell "find /sdcard/Pictures/ -type f | grep -v ".gs" | grep -v ".th""'
#         OutPictures = subprocess.Popen(Pictures,shell=True,stdout=subprocess.PIPE)
#         #微信历史 28
#         WeixinHistory='adb shell "find /sdcard/tencent/micromsg/weixin/ -type f"'
#         OutWeixinHistory = subprocess.Popen(WeixinHistory,shell=True,stdout=subprocess.PIPE)
#         #qq保存 66
#         QQSave='adb shell "find /sdcard/Tencent/QQ_Images/ -type f"'
#         OutQQSave = subprocess.Popen(QQSave,shell=True,stdout=subprocess.PIPE)
#         #童话录音
#         # CallRecording = 'adb shell "find /sdcard/MIUI/sound_recorder/ -type f | grep -v "nomedia" | sed "s///g" | sed "s/ //g""'
#         CallRecording = 'adb shell "find /sdcard/MIUI/sound_recorder/ -type f | grep -v "nomedia""'
#         OutCallRecording = subprocess.Popen(CallRecording,shell=True,stdout=subprocess.PIPE)
#
#         #连接数据库
#         db=pymysql.connect(host='114.115.248.128',user='root',password='Esxi0000.',db='redmi_k40')
#         cursor = db.cursor()
#
#         #参数：1，subprocess.Popen读出的文件列表
#         #参数：2，备份存放位置 格式：" D:\\Jaina\\Audio\\和" D:\\Jaina\\Image\\
#         def backup(list,fileDir,backupDir):
#             global ren,backupNum,rebackupNum,allNum
#
#             while True:
#                 allNum = allNum + 1
#                 #手机文件列表 读取每一行，并去掉\r,\n
#                 fileList = list.stdout.readline().decode().strip('\n').strip('\r')
#                 #文件名称列表
#                 try:
#                     fileListName = fileList.rsplit("/",1)[1].replace(' ','')
#                 #如果文件列表读不到东西，则表示列表为空或者读到了最后
#                 except:
#                     #减去空行 不然统计数量会变多
#                     allNum = allNum - 1
#                     print("此行为空!函数运行结束或者请求运行结束！")
#                 #查询数据库中是否存在记录
#                 selectFile = "select filedir from backup where filedir=%s"
#                 cursor.execute(selectFile,fileList)
#                 selectmsg = cursor.fetchall()
#                 #数据库存在记录
#                 if selectmsg:
#                     #print(line.rsplit("/",1)[1].replace(' ','')+" 文件数据库中已存在!")
#                     #到硬盘去验证文件是否真的存在
#                     fileInDisk = 'dir '+backupDir+fileListName+'/s /b'
#                     OutfileInDisk = subprocess.Popen(fileInDisk,shell=True,stdout=subprocess.PIPE).stdout.readline()
#                     #硬盘中存在
#                     if OutfileInDisk:
#                         print(fileListName+" 已经备份过了!")
#                     else:
#                         rebackupNum=rebackupNum+1
#                         print(fileListName+" 硬盘中的文件消失!正在重新备份!")
#                         #复制文件 拼接adb pull命令 将复制出的文件名称去除空格 复制时指定文件名
#                         cpFile = 'adb pull "'+fileList+fileDir+fileListName
#                         out = subprocess.Popen(cpFile,shell=True,stdout=subprocess.PIPE)
#                         print(out.stdout.read().decode())
#                 #数据库不存在记录 - 插入并复制
#                 else:
#                     #插入新行 如果这一行不为空，则将此行插入数据库和备份
#                     if fileList:
#                         #预编译
#                         sql = "insert into backup(filedir) values(%s)"
#                         #执行语句
#                         cursor.execute(sql,fileList)
#                         #复制文件 拼接adb pull命令 将复制出的文件名称去除空格 复制时指定文件名
#                         #cpFile = 'adb pull "'+line+'" D:\\Jaina\\Audio\\'+line.rsplit("/",1)[1].replace(' ','')
#                         cpFile = 'adb pull "'+fileList+fileDir+fileListName
#                         out = subprocess.Popen(cpFile,shell=True,stdout=subprocess.PIPE)
#                         print(out.stdout.read().decode())
#                         #提交数据
#                         db.commit()
#                         backupNum=backupNum+1
#                         print(fileListName+" 是新文件，已经备份好了!")
#                     #新行为空
#                     else:
#                         break
#             ren = {'msg':'新备份了'+str(backupNum)+'个文件!'+'重新备份了'+str(rebackupNum)+'个文件!'+'共有备份文件'+str(allNum)+'个!','msg_code':200}
#         # backup(OutDCIM,'" D:\\Jaina\\Image\\','D:\Jaina\\')
#         # backup(OutPictures,'" D:\\Jaina\\Image\\','D:\Jaina\\')
#         # backup(OutWeixinHistory,'" D:\\Jaina\\Image\\','D:\Jaina\\')
#         # backup(OutQQSave,'" D:\\Jaina\\Image\\','D:\Jaina\\')
#         backup(OutCallRecording,'" D:\\Jaina\\Audio\\','D:\Jaina\\')
#
#     else:
#         ren = {'msg':'这里是法师吉安娜!','msg_code':200}
#
#     return json.dumps(ren,ensure_ascii=False)
#
#
#
# if __name__ == '__main__':
#     api.run(port=8888,debug=True,host='127.0.0.1')